package com.xcx.baseManger.controller;


import com.xcx.baseManger.helper.User;
import com.xcx.baseManger.service.MessageService;
import com.xcx.commoncore.request.logrequest.RegisterMobileRequest;
import com.xcx.commoncore.request.messagerequest.MessageFIindByUsername;
import com.xcx.commoncore.request.messagerequest.QueryMessageListRequest;
import com.xcx.commonuntils.returnuntls.ErrorEnum;
import com.xcx.commonuntils.returnuntls.ReturnMessage;
import com.xcx.commonuntils.returnuntls.ReturnMessageUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Objects;

@Api(tags = "IM-mongdb-api")
@RestController
@RequestMapping("/message")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Slf4j
public class MessgeController {

    final private MessageService messageService;


    @PostMapping("/queryMessageList")
    @ApiOperation(value = "获取聊天记录")
    public ReturnMessage<Object> queryMessageList(@RequestBody @Valid QueryMessageListRequest registerVo, BindingResult br) {

        log.info("Request:{}", registerVo);
        if (registerVo == null) {
            return ReturnMessageUtils.errorWithEum(ErrorEnum.E_REQUEST_PARAM_ERRORJSON);
        }
        if (br.hasErrors()) {
            return ReturnMessageUtils.error(ErrorEnum.E_REQUEST_PARAM_ERROR.getErrorCode(), Objects.requireNonNull(br.getFieldError()).getDefaultMessage());
        }

        return messageService.queryMessageList(registerVo);
    }



//    @PostMapping("/queryFriendsList")
//    @ApiOperation(value = "获取好友列表")
//    public ReturnMessage<Object> queryFriendsList(@RequestBody @Valid QueryMessageListRequest registerVo, BindingResult br) {
//
//        log.info("Request:{}", registerVo);
//        if (registerVo == null) {
//            return ReturnMessageUtils.errorWithEum(ErrorEnum.E_REQUEST_PARAM_ERRORJSON);
//        }
//        if (br.hasErrors()) {
//            return ReturnMessageUtils.error(ErrorEnum.E_REQUEST_PARAM_ERROR.getErrorCode(), Objects.requireNonNull(br.getFieldError()).getDefaultMessage());
//        }
//
//        return messageService.queryFriendsList(registerVo);
//    }
















    @PostMapping("/deleteUser")
    public void deleteUser(@RequestBody MessageFIindByUsername request) {
        log.info("Request:{}", request);

        messageService.deleteById(request.getUsername());

    }


    @PostMapping("/findByUsername")
    public User findByUsername(@RequestBody MessageFIindByUsername request) {
        log.info("Request:{}", request);

        return messageService.findByUsername(request.getUsername());

    }


}
